package com.seth.myapplication.surface;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.seth.myapplication.R;
import com.seth.myapplication.bean.Rewords;
import com.seth.myapplication.bean.User;
import com.seth.myapplication.service.NetWorkService;
import com.seth.myapplication.utils.TaskSetFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Created by Seth on 2017/11/20.
 */

public class UsrGetTask extends TaskSetFragment {
    public static final String RECEIVER_INIT = "com.seth.myapplication.surface.MY_ACCEPT_TASK";
    public static final String GET_USER = "com.seth.myapplication.surface.UsrGetTask";
    private int page = 1;
    private int count = 15;
    private NetWorkService service;
    private int Index=0;
    private User mUser=null;


    @Override
    public View initStartView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_task_show,container,false);
    }

    @Override
    public void getNetTask() {
        service = new NetWorkService(getActivity(),RECEIVER_INIT);
        String data_str = mUser.getUsrId()+"";
        String url = "http://139.199.9.252/pc/task";
        service.freshTask(data_str,url,"userId");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        mUser = (User) bundle.getSerializable(GET_USER);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public String newBroadCast() {
        return RECEIVER_INIT;
    }

    @Override
    protected ArrayList<Rewords> DealUsers(ArrayList<Rewords> rewords, HashMap<String, User> users, Intent intent) {
        //添加同步互斥
        synchronized(UsrGetTask.class) {
            User user = (User) intent.getSerializableExtra("usrInfo");

            //写到这里未完待续
            if (user != null) {
                Index++;
                users.put(user.getUsrId() + "", user);
            }
        }
        if(Index==users.size()){
            Index=0;
            for(int i=0;i<rewords.size();i++){
                User user1 = users.get(rewords.get(i).getUsrId()+"");
                rewords.get(i).setGeter(user1);
            }
            refreshTask.finishRefresh();
            taskItemAdapter.setFlag(3);
            taskItemAdapter.setList(rewords);
            taskItemAdapter.notifyDataSetChanged();
        }

        return rewords;

    }

    @Override
    protected HashMap<String, User> DealRewords(ArrayList<Rewords> rewords) {
        HashMap<String, User> users = new HashMap<>();
        Iterator<Rewords> i = rewords.iterator();
        //将悬赏的用户以id为key存入map
        while (i.hasNext()){
            Rewords r = i.next();
            if(r.getGeterId()!=0)
                Log.i("okhttpRespond","获取id"+ i+"用户信息");
                users.put(r.getGeterId()+"",null);
        }
        //获取有关key的Set
        Iterator<String> iterator = users.keySet().iterator();
        while(iterator.hasNext()){
            service.getUser(iterator.next());
        }
        if(users.isEmpty()){
            refreshTask.finishRefresh();
            taskItemAdapter.setList(rewords);
            taskItemAdapter.notifyDataSetChanged();
        }
        return users;

    }

    public static UsrGetTask newInstance(User user) {
        UsrGetTask fragment = new UsrGetTask();
        Bundle bundle = new Bundle();
        bundle.putSerializable(GET_USER,user);
        fragment.setArguments(bundle);
        return fragment;
    }
}
